package com.hn.gege.core.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hn.gege.core.modules.sys.entity.DatabaseRecord;
import com.hn.gege.core.modules.sys.mapper.DatabaseRecordMapper;
import com.hn.gege.core.modules.sys.service.IDatabaseRecordService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 数据库操作记录 服务实现类
 * </p>
 *
 * @author zxy
 * @since 2022-05-27
 */
@Service
public class DatabaseRecordServiceImpl extends ServiceImpl<DatabaseRecordMapper, DatabaseRecord> implements IDatabaseRecordService {


    @Override
    public List<Map<String, Object>> datas(String tableName, String whereSql) {
        List<Map<String, Object>> datas = baseMapper.datas(tableName, whereSql);
        if (!CollectionUtils.isEmpty(datas)) {
            datas.forEach(item -> {
                // 如果表不存在id,将第一个字段作为id
                if (!item.containsKey("id")) {
                    for (Map.Entry<String, Object> e :item.entrySet()) {
                        item.put("id", e.getValue());
                        break;
                    }
                }
            });
        }
        return datas;
    }
}
